<template>
	<a-row :gutter="10" class="mb-2" v-if="!props.hiddenFlag">
		<a-col :span="24">
			<a-card>
				<a-space>
					<a-input-search placeholder="请输入内容" v-model:value="config.search"
									@search="refresh"></a-input-search>
					<a-button @click="add" type="primary">新增</a-button>
				</a-space>
			</a-card>
		</a-col>
	</a-row>

	<a-row :gutter="10" class="mb-2" v-if="!props.hiddenFlag">
		<a-col :span="24">
			<a-card>
				<h-table ref="tableRef" :columns="columns" :data="queryProcessList"></h-table>
			</a-card>
		</a-col>
	</a-row>

	<drag-modal
		:closable="false"
		v-model:visible="config.processDiag.visible"
		@cancel="config.processDiag.visible = false"
		@ok="saveProcess"
		width="300px"
	>
		<a-form :model="config.processDiag">
			<a-form-item label="流程名称">
				<a-input v-model:value="config.processDiag.processName"
						 @change="nameNameToCode"
						 autocomplete="off"></a-input>
			</a-form-item>
			<a-form-item label="流程编码">
				<a-input v-model:value="config.processDiag.processCode" autocomplete="off"></a-input>
			</a-form-item>
			<a-form-item label="流程描述">
				<a-input v-model:value="config.processDiag.description" autocomplete="off"></a-input>
			</a-form-item>
			<a-form-item label="日志级别">
				<a-select v-model:value="config.processDiag.logLevelCode" placeholder="Select">
					<a-select-option v-for="item in config.logLevelList" :key="item.code" :label="item.name"
									 :value="item.code"/>
				</a-select>
			</a-form-item>

			<a-form-item label="引导线程池">
				<a-select v-model:value="config.processDiag.bootThreadCode" placeholder="Select">
					<a-select-option
						v-for="item in config.threadPoolList"
						:key="item.threadCode"
						:label="item.threadName"
						:value="item.threadCode"
					/>
				</a-select>
			</a-form-item>

			<a-form-item label="预执行线程池">
				<a-select v-model:value="config.processDiag.prepareThreadCode" placeholder="Select">
					<a-select-option
						v-for="item in config.threadPoolList"
						:key="item.threadCode"
						:label="item.threadName"
						:value="item.threadCode"
					/>
				</a-select>
			</a-form-item>

			<a-form-item label="执行线程池">
				<a-select v-model:value="config.processDiag.executeThreadCode" placeholder="Select">
					<a-select-option
						v-for="item in config.threadPoolList"
						:key="item.threadCode"
						:label="item.threadName"
						:value="item.threadCode"
					/>
				</a-select>
			</a-form-item>

			<a-form-item label="数据流存储模式">
				<a-select v-model:value="config.processDiag.rowSetTag" placeholder="Select">
					<a-select-option v-for="item in config.rowSetList" :key="item.code" :label="item.name"
									 :value="item.code"/>
				</a-select>
			</a-form-item>

			<a-form-item label="数据库" v-if="config.processDiag.rowSetTag=='db'">
				<a-select v-model:value="config.processDiag.rowSetTagDataSourceId" class="m-2" clearable filterable>
					<a-select-option v-for="item in dataSourceList" :value="item.id" :key="item.id"
									 label="item.datasourceName" />
				</a-select>
			</a-form-item>
			<!--        <a-form-item label="流程版本">-->
			<!--          <a-input v-model="processDiag.version" autocomplete="off"></a-input>-->
			<!--        </a-form-item>-->
		</a-form>
	</drag-modal>

	<a-modal :closable="false" :footer="null" v-model:open="config.processVersionDiag" width="100%">
		<ProcessVersionManager :processId="config.processId" :processVersionCallback="processVersionCallback"/>
	</a-modal>
</template>

<script setup>
import ProcessVersionManager from '@/views/process/ProcessVersionManager.vue'
// import apis from "@/request/apis.js";
import processApi from '@/api/process/processApi'
import threadApi from '@/api/thread/threadApi'
import {message} from 'ant-design-vue'
import dataSourceApi from "@/api/datasource/dataSourceApi";

const props = defineProps(['groupId', 'productId', 'hiddenFlag', 'processVersionCallback', 'operaCallback'])
let processVersionCallback = ref()

const dataSourceList =ref([])
const tableRef = ref()

const columns = ref([
	{
		title: '名称',
		key: 'processName',
		dataIndex: 'processName',
		ellipsis: true
	},
	{
		title: '编码',
		key: 'processCode',
		dataIndex: 'processCode',
		ellipsis: true
	},
	{
		title: '描述',
		key: 'description',
		dataIndex: 'description',
		ellipsis: true
	},
	{
		title: '最后修改日期',
		key: 'modifyTime',
		dataIndex: 'modifyTime',
		ellipsis: true
	},
	{
		title: '操作',
		key: 'action',
		width: 220,
		dataIndex: 'action',
		ellipsis: true,
		fixed: 'right',
		buttonList: [
			{
				click: (field, record) => {
					handleClick(record)
				},
				render: (record) => {
					return '查看'
				}
			},
			{
				type: 'primary',
				click: (field, record) => {
					edit(record)
				},
				render: (record) => {
					return '修改'
				}
			},
			{
				click: (field, record) => {
					handleConfig(record)
				},
				render: (record) => {
					return '配置'
				}
			},
			{
				click: (field, record) => {
					handleRemove(record)
				},
				render: (record) => {
					return '删除'
				}
			}
		]
	}
])
const {proxy} = getCurrentInstance()

const config = ref({
	search: '',
	tableData: [],
	logLevelList: [],
	rowSetTagDataSourceId:null,
	groupId: null,
	processVersionDiag: false,
	processId: null,
	threadPoolList: [],
	rowSetList: [],
	initStr: '',
	processDiag: {
		visible: false,
		prepareThreadCode: '',
		processCode: '',
		description: '',
		logLevelCode: 'Basic',
		viewFlag: false,
		executeThreadCode: '',
		bootThreadCode: '',
		processName: '',
		groupId: null,
		id: null
	}
})

watch(
	() => props.groupId,
	(newValue, oldValue) => {
		config.value.groupId = props.groupId;
		refresh()
	}
)

const nameNameToCode = () => {

	if (!config.value.processDiag.processName) {
		return
	}
	config.value.processDiag.processCode = config.value.processDiag.processName.toPinyin(true);
}

onMounted(() => {
	config.value.groupId = props.groupId
	queryThreadPoolList()
	queryLogLevelList()
	queryRowSetList()
	 queryDbList();
	if (props.processVersionCallback) {
		processVersionCallback.value = (data) => {
			props.processVersionCallback(data)
			config.value.processVersionDiag = false
		}
	} else {
		processVersionCallback.value = null
	}
})

const queryDbList=async()=>{
	const data = await dataSourceApi.queryList()
	// console.log(data);
	// this.$set(this, "tableData", data);
	dataSourceList.value = data

}

const handleClick = async (row) => {
	await edit(row)
	config.value.processDiag.viewFlag = true
}
const edit = async (row) => {
	const data = await processApi.getById({id: row.id, version: row.version})
	config.value.processDiag = data
	config.value.processDiag.visible = true
}
const queryThreadPoolList = async () => {
	const data = await threadApi.list({
		disableFlag: 0
	})
	config.value.threadPoolList = data
}
const queryLogLevelList = async () => {
	const data = await processApi.queryLogLevelList({})
	config.value.logLevelList = data
}

const queryRowSetList = async () => {
	const data = await processApi.queryRowSetList({})
	config.value.rowSetList = data
}
const add = () => {
	if (!config.value.groupId) {
		message.error('请选择分组')
		return
	}
	config.value.processDiag = {}
	config.value.processDiag.visible = true
	config.value.processDiag.logLevelCode = 'Basic'
}
const handleRemove = async (row) => {
	await processApi.removeById({id: row.id})
	message.success('删除成功')
	refresh()
	if (props.operaCallback) {
		props.operaCallback('delete', 'process', row)
	}
}
const handleConfig = (row) => {
	config.value.processVersionDiag = true
	config.value.processId = row.id
	// nextTick(() => {
	// 	proxy.$refs.prv.queryList(row.id);
	// })
}
const saveProcess = async () => {
	if (config.value.processDiag.viewFlag) {
		config.value.processDiag.visible = false
		return
	}
	if (!config.value.processDiag.processName) {
		message.error('请输入名称')
		return
	}
	config.value.processDiag.groupId = config.value.groupId
	config.value.processDiag.productId = props.productId
	if (!config.value.processDiag.logLevelCode) {
		message.error('请选择日志级别')
		return
	}
	if (!config.value.processDiag.processCode) {
		message.error('请输入流程编码')
		return
	}
	let id = await processApi.save(config.value.processDiag)
	if (props.operaCallback) {
		props.operaCallback(config.value.processDiag.id ? 'edit' : 'add', 'process', {
			id,
			...config.value.processDiag
		})
	}

	config.value.processDiag = {}
	refresh()

}
const refresh = () => {
	if (props.hiddenFlag) {
		return
	}
	tableRef.value.refresh(true)
}
const queryProcessList = async () => {
	if (!config.value.groupId) {
		return
	}
	return await processApi.queryProcessList({groupId: config.value.groupId, productId: props.productId})
}
const show = (action, data) => {

	if (action == 'add') {
		config.value.groupId = data.id;
		add()
	} else if (action == 'remove') {
		handleRemove(data);
	} else if (action == 'edit') {
		config.value.groupId = data.groupId;
		edit(data);
	}
}

defineExpose({
	show
})
</script>

<style scoped></style>
